<?php
/* ------------------------------------------------------------------ */
// IMPORT
if (isset($_FILES['csvfile'])) {
	if (! $xoopsGTicket->check(TRUE, 'flatdata_admin_import')) {
		redirect_header(XOOPS_URL.'/',3,$xoopsGTicket->getErrors());
	}

	$uploadFileName = $_FILES['csvfile']['tmp_name'];

	if (is_uploaded_file($uploadFileName)) {
		$csv_data = file_get_contents($uploadFileName);
		$fp = tmpfile();
		fwrite($fp, $csv_data);
		rewind($fp);
	//	$line1 = fgets($fp);
	//	fclose($fp);
	//	$line1 = explode(',', $line1);
	//	if (count($line1) != 7) {
	//		redirect_header("$mydirurl/admin/index.php?page=import", 5, _MD_A_FLATDATA_IMPORT_ERROR_MSG);
	//		exit;
	//	}

	//	$uploadFileName = str_replace('\\', '/', $uploadFileName);// for windows
	//	$sql = 'LOAD DATA INFILE "' . $uploadFileName 
	//			. '" INTO TABLE ' . $xoopsDB->prefix($mydirname.'_data') 
	//			. ' FIELDS TERMINATED BY "," ENCLOSED BY "\""'; // LINES TERMINATED BY '\n';
	//	$r = $xoopsDB->query($sql);
	//	if (! $r) {
	//		$fp = tmpfile();
	//		fwrite($fp, $csv_data);
	//		rewind($fp);
			flock($fp, LOCK_SH);
			while (!feof($fp)) {
				$line = fgets($fp);
				$line_arr = explode(',', $line);
				if (count($line_arr) == 7) {
					$sql = 'INSERT INTO ' . $xoopsDB->prefix($mydirname.'_data') . ' SET ';
				//	$sql.= "`did`='"      . mysql_real_escape_string(trim($line_arr[0],'"\'')) . "', ";
					$sql.= "`data`='"     . mysql_real_escape_string(trim($line_arr[1],'"\'')) . "', ";
					$sql.= "`uid`='"      . mysql_real_escape_string(trim($line_arr[2],'"\'')) . "', ";
					$regidate = intval(trim($line_arr[3],'"\''));
					$regidate = empty($regidate) ? time(): $regidate;
					$sql.= "`regidate`='" . mysql_real_escape_string($regidate) . "', ";
					$sql.= "`embed`='"    . mysql_real_escape_string(trim($line_arr[4],'"\'')) . "', ";
					$sql.= "`cat_id`='"   . mysql_real_escape_string(trim($line_arr[5],'"\'')) . "', ";
					$sql.= "`hits`='"     . mysql_real_escape_string(trim($line_arr[6],'"\'')) . "'";
					$r = $xoopsDB->query($sql) or die('ERROR: IMPORT');
				}
			}
			fclose($fp);
	//	}
		redirect_header("$mydirurl/admin/index.php?page=import", 5, _MD_A_FLATDATA_IMPORT_COMPLETE_MSG);
		exit;
	}
}
/* ------------------------------------------------------------------ */


// Field
$allfields = $fields->getAllFields(TRUE);
$csvString = 'did,';
foreach ($allfields as $f) {
	$csvString .= '[field'. $f['fid'] .']' . $f['fname'] . '[/field'. $f['fid'] .']';
}
$csvString .= ',uid,regidate,embed,cat_id,hits';

$tableName = $xoopsDB->prefix($mydirname . '_data');
/* ------------------------------------------------------------------ */
xoops_cp_header() ;
include dirname(__FILE__).'/mymenu.php' ;

echo '<h3>'. htmlspecialchars($xoopsModule->getVar('name'), ENT_QUOTES, _CHARSET) .' - '. constant($constprefMI .'_ADMENU3') .'</h3>';
if (count($allfields) > 0) {
	echo sprintf(_MD_A_FLATDATA_IMPORT_MSG, $tableName);
	echo '<p style="border:1px solid #F99;margin:12px 0; padding:6px; background:#FEE;">';
	echo htmlspecialchars($csvString, ENT_QUOTES, _CHARSET);
	echo '</p>';
	echo '<form action="" method="post" enctype="multipart/form-data">';
	echo '<input type="file" name="csvfile" value="" /><br />';
	echo '<input type="submit" name="import_submit" value="'. _MD_A_FLATDATA_IMPORT_SUBMIT .'" />';
	echo $xoopsGTicket->getTicketHtml(__LINE__, 1800, 'flatdata_admin_import');
	echo '</form>';
} else {
	echo '<p style="border:1px solid #F99;margin:12px 0; padding:6px; background:#FEE; color:#C00;">';
	echo _MD_A_FLATDATA_IMPORT_REQUIRE_FIELD_MSG;
	echo '</p>';
}

xoops_cp_footer();
/* ------------------------------------------------------------------ */
